<?php

/**
 * Controller_Admin_Web 控制器
 */
class Controller_Admin_shopgoods extends Controller_Abstract {
	
	/**
	 * 商品
	 */
	function actionIndex()
	{
		$this->union_table = shopgoods::meta();
		$this->lists();
	}
	 
	/**
	 * 商品设置
	 */
	 
	function actionAdd() {
	    if($this->_context->isGET()){
	    	if(!empty($this->_context->id)){
	    		$goods = shopgoods::find('id=?',$this->_context->id)->getOne();
	    		$this->_view['goods'] = $goods;
	    	}
	    }
		if($this->_context->isPOST())
		{
			if(!empty($this->_context->id)){
				$goods = shopgoods::find('id=?',$this->_context->id)->getOne();
				$goods->name=trim($this->_context->name);
				$goods->cat_id=intval($this->_context->cat_id);
				$goods->tag_id=intval($this->_context->tag_id);
				$goods->marketprice=floatval($this->_context->marketprice);
				$goods->productprice = floatval($this->_context->productprice);
				$goods->stock = intval($this->_context->stock);
				$goods->weight = intval($this->_context->weight);
				$goods->ico1 = trim($this->_context->ico1);
				$goods->ico2 = trim($this->_context->ico2);
				$goods->ico3 = trim($this->_context->ico3);
				$goods->product_thumb=trim($this->_context->product_thumb);
				$goods->dec=trim($this->_context->dec);
				$goods->is_show=intval($this->_context->is_show);
				$goods->is_rec=intval($this->_context->is_rec);
				$goods->detail=trim($this->_context->detail);
				if($this->_context->serviceids){
					$goods->serviceids=implode(',',$this->_context->serviceids);
				}
				
		        $goods->save(0,'update');
			}else{
				$goods = new shopgoods;
				$goods->acct_id=$_SESSION['acct']['id'];
				$goods->name=trim($this->_context->name);
				$goods->cat_id=intval($this->_context->cat_id);
				$goods->tag_id=intval($this->_context->tag_id);
				$goods->marketprice=floatval($this->_context->marketprice);
				$goods->productprice = floatval($this->_context->productprice);
				$goods->stock = intval($this->_context->stock);
				$goods->weight = intval($this->_context->weight);
				$goods->ico1 = trim($this->_context->ico1);
				$goods->ico2 = trim($this->_context->ico2);
				$goods->ico3 = trim($this->_context->ico3);
				$goods->product_thumb=trim($this->_context->product_thumb);
				$goods->dec=trim($this->_context->dec);
				$goods->is_show=intval($this->_context->is_show);
				$goods->is_rec=intval($this->_context->is_rec);
				$goods->detail=trim($this->_context->detail);
				if($this->_context->serviceids){
					$goods->serviceids=implode(',',$this->_context->serviceids);
				}
				
				$goods->save(0,'create');
			}
			if(!$_POST['id'])	{
	            Syslog::logs ( '添加商品,id :' . $goods->id );
	        }else{
	            Syslog::logs ( '编辑商品,id :' . $goods->id );
	        }
	        $this->_context->forwardUrl = url("shopgoods/index");
			$this->success('编辑成功');
	     }
	}
	 
	/**
	 * 删除商品
	 */
	function actionDel() {
	     Syslog::logs("删除商品，ID：".print_r($this->_context->ids,true));
	     $this->union_table = shopgoods::meta();
	     $this->delete(array('id'=>$this->_context->ids));
	}
}